﻿using EnterpriseWebsiteEntity;
using EnterpriseWebsiteUtil.Helper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace EnterpriseWebsiteBLL
{
    public class ProductPriceCurrenciesService
    {
        public readonly SqlSugarClient _db;
        public readonly ProductService _productService;
        public readonly Mapper _mapper;

        public ProductPriceCurrenciesService(SqlSugarHelper sqlSugarHelper, ProductService productService, Mapper mapper)
        {
            _db = sqlSugarHelper.sugarClient;
            _productService = productService;
            _mapper = mapper;
        }

        public async Task InsertOrUpdate(IEnumerable<ProductPriceCurrenciesEntity>  productPriceUnits)
        {
            _db.BeginTran();
            foreach (var item in productPriceUnits)
            {
                try
                {
                    if (item.Id == 0)
                    {
                        if (string.IsNullOrEmpty(item.Unit))
                        {
                            continue;
                        }
                        item.CreateDate = DateTime.Now;
                        await _db.Insertable(item)
                            .ExecuteCommandAsync();
                    }
                    else
                    {
                        var entity = await GetProductPriceCurrenciesById(item.Id);
                        if (entity == null)
                        {
                            throw new Exception("商品货币单位找不到对象");
                        }
                        if (string.IsNullOrEmpty(item.Unit))
                        {
                            throw new Exception("商品货币单位不能为空");
                        }
                        entity.Unit = item.Unit;
                        await _db.Updateable(entity)
                            .ExecuteCommandAsync();
                    }
                }
                catch (Exception ex)
                {
                    _db.RollbackTran();
                    throw ex;
                }
            }
            _db.CommitTran();
        }

        public async Task<List<ProductPriceCurrenciesEntity>> GetAllProductPriceCurrencies()
        {
            return await _db.Queryable<ProductPriceCurrenciesEntity>()
                .ToListAsync();
        }

        public async Task<ProductPriceCurrenciesEntity> GetProductPriceCurrenciesById(int id)
        {
            return await _db.Queryable<ProductPriceCurrenciesEntity>()
                .Where(p => p.Id == id)
                .FirstAsync();
        }


        public async Task DeleteProductPriceCurrenciesById(int id)
        {
            var entity = await GetProductPriceCurrenciesById(id);
            if (entity == null)
            {
                throw new Exception($"删除失败，Id {id} 商品货币单位不存在");
            }
            else
            {
                var productList = await _productService.GetProductByCurrencies(entity.Id);
                if (productList.Count > 0)
                {
                    throw new Exception($"删除失败，“{entity.Unit}”商品货币单位存在{productList.Count}条商品，请删除商品或更换商品货币单位后再删除");
                }
            }
            await _db.Deleteable<ProductPriceCurrenciesEntity>()
                .Where(p => p.Id == id)
                .ExecuteCommandAsync();
        }
    }
}
